﻿Imports MySoft.Map.Workflow.Engine
Imports MySoft.Map.Workflow.Utility
Imports Mysoft.Map.Workflow.Public
Imports Mysoft.Map.Workflow.Business
Imports Mysoft.Map.Workflow.Types
Imports Mysoft.Map.Data
Imports System.Xml

Partial Public Class WF_Engine_XmlHttp
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        If Session("UserGUID") Is Nothing OrElse Session("UserGUID") = "" OrElse Session("MySessionState") Is Nothing OrElse Session("MySessionState") = "" Then
            MyBase.Response.ContentType = "Text/HTML"
            MyBase.Response.Clear()
            MyBase.Response.Write("服务器超时")
            MyBase.Response.End()
            Return
        End If

        Dim strYwType, strOnlyFlag, strTxt As String
        Dim strReturn As String = ""

        strYwType = Request.QueryString("ywtype")           '业务标识
        strOnlyFlag = Request.QueryString("ywonlyflag")     '唯一键值
        strTxt = Request.QueryString("ywtxt")               '其它参数值

        Select Case strYwType.ToUpper

            '-----------------------------流程监控 ---------------------------------------------------'
            Case "ProcessWatch_Close".ToUpper                   '流程监控-关闭流程
                strReturn = ProcessWatch.CloseProcess(strOnlyFlag)
                Exit Select

            Case "ProcessWatch_DeleteProcessEntity".ToUpper     '流程监控-删除流程
                strReturn = ProcessWatch.DeleteProcess(strOnlyFlag)
                Exit Select
                '-----------------------------------------------------------------------------------------'
                '-----------------------------重置责任人 ---------------------------------------------------'
            Case "AuditorReplace".ToUpper
                strReturn = ProcessAuditorReplace.Handle(strOnlyFlag, strTxt, getPostStr())
                Exit Select

            Case "MULTISTEPVERIFY"
                strReturn = ProcessAuditorReplace.MultiStepVerify(getPostStr())
                Exit Select
                '-----------------------------------------------------------------------------------------'

            Case ""
                Exit Select


                '-----------------------------流程发起 ---------------------------------------------------'
            Case "SAVEDOC"
                strReturn = ProcessInitiate.SaveDoc(getPostStr())
                Exit Select
            Case "SAVEDRAFT"
                Dim strFlag As String '校验DomainXml是否为空
                Dim dataXml As String = getPostStr()
                strFlag = verifyDomainXmlIsNull("SAVEDRAFT", dataXml)
                If strFlag = "" Then
                    strReturn = ProcessInitiate.SaveDraft(dataXml)
                Else
                    strReturn = strFlag
                End If
                Exit Select

            Case "INITIATEPROCESS"
                Dim strFlag As String '校验DomainXml是否为空
                Dim dataXml As String = getPostStr()
                strFlag = verifyDomainXmlIsNull("INITIATEPROCESS", dataXml)
                If strFlag = "" Then
                    strReturn = ProcessInitiate.Initiate(dataXml)
                Else
                    strReturn = strFlag
                End If
                Exit Select
            Case "DELETEPROCESSENTITY"
                strReturn = ProcessInitiate.DeleteDraft(strTxt)
                Exit Select

                'BUG 45159 因并发造成发起提示出错 wangf 2009年4月27日10:10:58
                'ADD
            Case "TOGETHERVALIDATION"
                strReturn = ProcessInitiate.TogetherValidation(strOnlyFlag)
                'BUG 45159 END
                '-----------------------------------------------------------------------------------------'



                '-----------------------------流程流转 ---------------------------------------------------'
            Case "ProcessGuide_GetStepDisApproveGuide".ToUpper     '获取不同意操作向导XML
                strReturn = StepDisApproveGuide.GetStepDisApproveGuide(strOnlyFlag, strTxt)
                Exit Select

            Case "GetStepPassGuideXML".ToUpper
                Dim stepGUID, lastStationGUID, bt_DomainXML As String
                lastStationGUID = Request.QueryString("lastStationGUID")
                stepGUID = Request.QueryString("stepGUID")
                bt_DomainXML = getPostStr()

                strReturn = StepPassGuide.GetGuideXML(stepGUID, lastStationGUID, bt_DomainXML)
                Exit Select

            Case "GetOwnerAssignGuideXML".ToUpper
                Dim stepGUID, lastStationGUID, bt_DomainXML As String
                lastStationGUID = Request.QueryString("lastStationGUID")
                stepGUID = Request.QueryString("stepGUID")
                bt_DomainXML = getPostStr()

                strReturn = OwnerAssignGuide.GetGuideXML(stepGUID)
                Exit Select

            Case "ProcessHandle".ToUpper   '流转处理
                Dim strFlag As String '校验DomainXml是否为空
                Dim dataXml As String = getPostStr()
                strFlag = verifyDomainXmlIsNull("ProcessHandle", dataXml)
                If strFlag = "" Then
                    strReturn = ProcessHandle.Handle(dataXml)
                Else
                    strReturn = strFlag
                End If
                Exit Select

            Case "CheckStepIsFinish".ToUpper        '判断步骤是否完成
                Dim CurrentNodeNum As String = Request.QueryString("CurrentNodeNum")
                Dim StepType As StepType = [Enum].Parse(GetType(StepType), Request.QueryString("StepType"), True)
                Dim NodeGUID As String = Request.QueryString("NodeGUID")

                strReturn = StepPathUtil.GetNextNodeNum(strOnlyFlag, CurrentNodeNum, StepType, NodeGUID)
                Exit Select

            Case "GetToday".ToUpper                 '获取当前时间
                strReturn = MyDB.GetDataItemString("select convert(varchar(16),getdate(),121)")
                Exit Select

            Case "GetSelectAuditorsXml".ToUpper     '获取选择具体责任人界面XML
                strReturn = AuditorGuide.GetSelectAuditorsXml(strOnlyFlag, strTxt, getPostStr())
                Exit Select

            Case "GetSysActiveNumber".ToUpper       '获取当前流程待办的计数 
                strReturn = ProcessDisplay.GetSysActiveNumber(strOnlyFlag, Session("UserGUID"))
                Exit Select

                '-----------------------------------------------------------------------------------------'

                '--------------------------------协商-----------------------------------------------------'
            Case "GetAssignUserXML".ToUpper '获取已协商人员
                Dim nodeGUID As String
                nodeGUID = Request.QueryString("nodeGUID")
                strReturn = ProcessConsult.GetConsultUserXML(nodeGUID)
                Exit Select
            Case "Assign".ToUpper '发起协商
                strReturn = ProcessConsult.Consult(getPostStr)
                Exit Select
            Case "AssignSubmit".ToUpper '提交协商
                strReturn = ProcessConsult.ConsultSubmit(getPostStr())
                Exit Select
                '-----------------------------流程作废 ---------------------------------------------------'
            Case "GetCancelCCXML".ToUpper '获取作废要抄送人的人员
                Dim stepGUID As String, cancelType As String, processGuid As String, lastStationGuid
                stepGUID = Request.QueryString("stepGUID")
                processGuid = Request.QueryString("processGuid")
                lastStationGuid = Request.QueryString("lastStationGuid")
                cancelType = Request.QueryString("type")
                strReturn = ProcessCancel.GetCancelCCXML(processGuid, stepGUID, lastStationGuid, cancelType)
                Exit Select
            Case "InitiatorCancel".ToUpper '发起人作废
                strReturn = ProcessCancel.InitiatorCancel(getPostStr())
                Exit Select
            Case "WatcherCancel".ToUpper '监控人作废
                strReturn = ProcessCancel.WatcherCancel(getPostStr())
                Exit Select
            Case "HandlerCancel".ToUpper '处理人作废
                strReturn = ProcessCancel.HandlerCancel(getPostStr())
                Exit Select
            Case "BusinessCancel".ToUpper '业务作废
                strReturn = ProcessCancel.BusinessCancel(getPostStr())
                Exit Select
                '-----------------------------流程撤回 ---------------------------------------------------'

        End Select

        MyBase.Response.ContentType = "text/HTML"
        MyBase.Response.Clear()
        MyBase.Response.Write(strReturn)
        MyBase.Response.End()
    End Sub

    '功能：获取采用Post方式Send过来的值
    Private Function getPostStr() As String

        Dim b(Request.InputStream.Length - 1) As Byte
        Request.InputStream.Read(b, 0, Request.InputStream.Length)

        Return System.Text.Encoding.UTF8.GetString(b)

    End Function

    '功能：校验DomainXml是否为空
    ' 返回值：非空，返回空字符串
    '         空，返回具体错误信息xml
    Function verifyDomainXmlIsNull(ByVal type As String, ByVal dataXml As String) As String
        Dim processGUID, processModuleGUID, nodeGUID, DomainXml, isNew As String
        Dim strLog As String
        Dim documentType As Int32
        Dim p As Mysoft.Map.Workflow.Entity.Process
        Dim n As Mysoft.Map.Workflow.Entity.INode
        Dim strReturn As String
        strReturn = "" '默认返回“”
        Dim xmlDoc As XmlDocument = New XmlDocument()
        xmlDoc.LoadXml(dataXml)

        If Not xmlDoc.SelectSingleNode("//Params/ProcessGUID") Is Nothing Then
            processGUID = xmlDoc.SelectSingleNode("//Params/ProcessGUID").InnerText
        End If

        If Not xmlDoc.SelectSingleNode("//Params/PMGUID") Is Nothing Then
            processModuleGUID = xmlDoc.SelectSingleNode("//Params/PMGUID").InnerText
        End If

        If Not xmlDoc.SelectSingleNode("//Params/IsNew") Is Nothing Then
            isNew = xmlDoc.SelectSingleNode("//Params/IsNew").InnerText
        End If

        If Not xmlDoc.SelectSingleNode("//Params/NodeGUID") Is Nothing Then
            nodeGUID = xmlDoc.SelectSingleNode("//Params/NodeGUID").InnerText
        End If

        If Not xmlDoc.SelectSingleNode("//Params/DomainXml") Is Nothing Then
            DomainXml = xmlDoc.SelectSingleNode("//Params/DomainXml").InnerText.ToString()
        End If

        'DomainXml不为空时，直接返回
        If DomainXml <> "" Then
            Return ""
        End If

        If type = "ProcessHandle" Then  '流程处理
            n = Mysoft.Map.Workflow.Entity.NodeFactory.GetNodeEntity(nodeGUID)
            p = New Mysoft.Map.Workflow.Entity.Process(New Mysoft.Map.Workflow.Entity.StepPath(n.StepPathGUID).ProcessGUID)
            documentType = Convert.ToInt32(p.DocumentType)
            strLog = ";processGUID:" + p.ProcessGUID + ";nodeGUID" + nodeGUID
        Else '流程发起或者流程草稿
            If (isNew = "1") Then '新建流程
                documentType = MyDB.GetDataItemInt("SELECT DocumentType FROM dbo.myWorkflowProcessModule WHERE ProcessGUID ='" + processModuleGUID + "'")
            Else
                p = New Mysoft.Map.Workflow.Entity.Process(processGUID)
                documentType = Convert.ToInt32(p.DocumentType)
            End If

            strLog = "processModuleGUID:" + processModuleGUID + ";processGUID:" + processGUID
        End If


        'HTML表单和扩展表单才需要检测
        If documentType = 0 OrElse documentType = 2 Then
            If DomainXml = "" Then
                Log.Write(type, strLog)
                strReturn = "<xml result='false' errormessage='处理失败，业务数据丢失，请与系统管理员联系！'></xml>"
            End If
        End If

        Return strReturn

    End Function
End Class